// pages/goodsDetail/index.js
import { addCart, getAllProduction, getGoodsDetail, getGoodsService, getProduction, resetCart } from "../../service/index.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods_Id: '',
    detail: {},
    goods_images: [],
    service: [],
    total: {},
    comments: [],
    limit: 2,
    scoreType: '-1',
    page: 1,
    currentIndex: 0, // 当前轮播图索引
    show: false,
    isShow: false,
    buttonText: '',
    count: 1
  },
  onLoad: function (options) {
    console.log(options.query);
    // 获取 query 中的参数
    this.setData({
      goods_Id: options.query
    })
    this.getParameterDetails()
  },
  swiperChange: function (e) {
    this.setData({
      currentIndex: e.detail.current
    })
  },
  addCart: function (e) {
    // console.log(e);
    const { dataset } = e.currentTarget
    const buttonText = dataset
    console.log(buttonText);
    if (!wx.getStorageSync('token')) {
      wx.showToast({ title: '请先登录！', icon: 'none' })
    }

    addCart(this.data.detail.goods_id, this.data.count, (res) => {
      console.log(res, 2222)
      this.setData({
        isShow: false
      })
      resetCart(this.data.detail.goods_id, this.data.count, (res) => {
        console.log(res, 777777)
      })
    })


  },
  buyNow() {
    wx.navigateTo({
      url:`/pages/orderSettlement/index?totalPrice=${this.data.count*this.data.detail.goods_price_min*100}&cartTotal=${this.data.count}&goodsId=${this.data.detail.goods_id}&data=${JSON.stringify({
        "mode": "buyNow",
        "delivery": 10,
        "payType": 10,
        "couponId": 0,
        "isUsePoints": 0,
        "goodsId": this.data.detail.goods_id,
        "goodsNum": this.data.count,
        "goodsSkuId": this.data.detail.skuList[0].goods_sku_id
      })}`
    })
  },

  getParameterDetails: function () {
    // 获取 data 中的 Id
    const goods_Id = this.data.goods_Id
    const limit = parseInt(this.data.limit, 10);
    // console.log('111',goods_Id);
    // 调用接口数据
    getGoodsDetail(goods_Id, (data) => {
      // 处理成功返回的数据
      // console.log("Goods details:", data)
      const { detail } = data.data
      // console.log(detail);
      this.setData({
        detail: detail,
        goods_images: detail.goods_images,
      })
      // console.log(this.data.goods_images);
      // console.log(detail);
    }),
      getGoodsService(goods_Id, (data) => {
        // console.log(data, 999);
        const { list } = data.data
        this.setData({
          service: list
        })
        // console.log(this.data.service);
      }),
      // 商品评价总数
      getAllProduction(goods_Id, (data) => {
        // console.log(111,data);
        const { total } = data.data
        this.setData({
          total: total
        })
        // console.log(this.data.total);
      }),
      // 商品评价
      getProduction(goods_Id, limit, (data) => {
        // console.log("Goods comments:", data);
        const { list } = data.data
        this.setData({
          comments: list
        })
        // console.log(this.data.comments)
      })
  },
  toComment() {
    const goods_Id = this.data.goods_Id;
    wx.navigateTo({
      url: `/pages/comments/index?goodsId=${goods_Id}`,
    });
  },
  popupNotification(e) {
    // console.log(this.data.service);
    this.setData({
      show: true
    })
  },
  onClick(e) {
    const buttonText = e.detail._relatedInfo.anchorTargetText
    this.setData({
      isShow: true,
      buttonText: buttonText
    })
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  onCloseBtn() {
    this.setData({
      isShow: false
    })
  },
  countAdd() {
    const count = this.data.count
    this.setData({
      count: count + 1
    })
  },
  countReduce() {
    const count = this.data.count
    if (count > 1) {
      this.setData({
        count: count - 1
      })
    }
  }
})
